//
//  RCDiscountDataModel.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-6-25.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCDiscountDataModel.h"
#import "FrameworkRelativeDefines.h"
#import "NSArray+EX.h"
#import "NSString+NSString_Ex_.h"

@implementation RCDiscountDetail

@synthesize itemID = _itemID;
@synthesize itemName = _itemName;
@synthesize recommendWord = _recommendWord;
@synthesize tagName = _tagName;
@synthesize distance = _distance;
@synthesize picUrl   = _picUrl;

-(id)init{
    self = [super init];
    if (nil == self) {
        return nil;
    }
    self.itemID = nil;
    self.itemName = nil;
    self.recommendWord = nil;
    self.tagName = nil;
    self.distance = nil;
    return self;
}

-(void)dealloc{
    self.itemID = nil;
    self.itemName = nil;
    self.recommendWord = nil;
    self.tagName = nil;
    self.distance = nil;
    self.picUrl = nil;
    [super dealloc];
}

+(id)parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCDiscountDetail *retObject = nil;
    retObject = [[RCDiscountDetail alloc]init];
    if (nil == retObject) {
        return nil;
    }
    retObject.itemID = [data stringValueForKey:@"id" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.itemName = [data stringValueForKey:@"name" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.recommendWord = [data stringValueForKey:@"recommend" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.tagName = [data stringValueForKey:@"labels" defaultValue:nil operation:NSStringOperationTypeTrim];
    retObject.picUrl = [data stringValueForKey:@"picurl" defaultValue:nil operation:NSStringOperationTypeTrim];

    
    CGFloat distance = [data floatValueForKey:@"distance" defaultValue:0.0];
    NSString *distanceStr = nil;
    if (distance > 1000) {
        if (distance/1000 > 999) {
            distanceStr = @"999+km";
        }else{
            distanceStr = [NSString stringWithFormat:@"%.2fkm",distance/1000];
        }
        
    }else{
       distanceStr = [NSString stringWithFormat:@"%dm",(NSInteger)distance];
    }
    retObject.distance = distanceStr;
    return [retObject autorelease];
}

@end

@implementation RCDiscountDataModel

@synthesize totalCount = _totalCount;
@synthesize currentOffset = _currentOffset;
@synthesize currentReturnNum = _currentReturnNum;
@synthesize label = _label;
@synthesize itemArray = _itemArray;

-(id)init{
    self = [super init];
    if (nil == self) {
        return nil;
    }
    self.totalCount = 0;
    self.currentOffset = 0;
    self.currentReturnNum = 0;
    self.itemArray = nil;
    self.label = nil;
    return self;
}

-(void)dealloc{
    self.itemArray = nil;
    self.label = nil;
    [super dealloc];
}

+(id) parseDataFromJSON:(NSDictionary *)data{
    if (nil == data) {
        return nil;
    }
    RCDiscountDataModel *retObject = nil;
    retObject = [[RCDiscountDataModel alloc]init];
    if (nil == retObject) {
        return nil;
    }
    NSDictionary *dictData = [data dictionaryValueForKey:@"data" defaultValue:nil];
    if (nil == dictData) {
        [retObject release];
        return nil;
    }
    NSInteger total = [dictData intValueForKey:@"total" defaultValue:0];
    NSInteger offset = [dictData intValueForKey:@"pn" defaultValue:0];
    NSInteger returnNum = [dictData intValueForKey:@"rn" defaultValue:0];
    NSString *label = [dictData stringValueForKey:@"label" defaultValue:nil operation:NSStringOperationTypeTrim];
    
    retObject.totalCount = total;
    retObject.currentOffset = offset;
    retObject.currentReturnNum = returnNum;
    retObject.label = label;
    
    NSArray *dataArray = [dictData arrayValueForKey:@"items" defaultValue:nil];
    
    if (dataArray != nil) {
        
        NSMutableArray *itemArray = [[NSMutableArray alloc]initWithCapacity:[dataArray count]];
        for (NSDictionary *itemDict in dataArray) {
            RCDiscountDetail *item = [RCDiscountDetail parseDataFromJSON:itemDict];
            if (item != nil) {
                [itemArray addObject:item];
            }
        }
        retObject.itemArray = itemArray;
        [itemArray release];
    }
    
    
    
    return [retObject autorelease];
}

@end
